<template>
  <div class="content">
    <column-filter v-model="columnFilter"></column-filter>
    <div class="sync">
      <router-link to="/k-line-sync" v-if="isInElectron">
        一键下载全部
      </router-link>
      <router-link to="/download-client" v-else title="下载客户端">下载客户端,一键下载全部</router-link>
    </div>
    <datatable-local :searchable='true' :initData="initData" :columns="columns" :init-limit="30" :init-order-by="[[0, 'desc']]" :columnFilter="columnFilter"></datatable-local>
  </div>
</template>
<script>
import {
  isInElectron
} from 'libs/utils'
import {
  DatatableLocal,
  Searches
} from 'components/Datatables'
let {
  ColumnFilter
} = Searches
export default {
  components: {
    DatatableLocal,
    ColumnFilter
  },
  data: function () {
    return {
      qfq: false,
      initData: () => {
        return this.$http.get('//cdn.kuaiyudian.com/k-line/daily/index.txt').then((res) => {
          return res.text()
        }).then((txt) => {
          return txt.split('\n').filter((s) => s.trim() !== '').map((line) => {
            let arr = line.split(',')
            return {
              date: arr[0].substring(0, 10),
              path: 'daily/' + arr[0],
              size: parseInt(arr[1]),
              hash: arr[2]
            }
          })
        })
      },
      isInElectron: isInElectron(),
      columnFilter: [{
        id: 'year',
        name: 'date',
        label: '年份',
        filter: 'select-btn',
        options: [2017, 2016, 2015, 2014],
        searchType: 'year'
      }, {
        name: 'date',
        label: '月份',
        filter: 'select-btn-month',
        searchType: 'month',
        valueType: parseInt
      }],
      columns: [{
        name: 'date',
        title: '日期'
      }, {
        title: '类型',
        data: '日K',
        searchable: false
      }, {
        title: '操作',
        rawHtml: true,
        data: function () {
          if (isInElectron()) {
            return `<a href="//cdn.kuaiyudian.com/k-line/${this.path} ">下载</a>`
          } else {
            return `<a target="_blank" href="//cdn.kuaiyudian.com/k-line/${this.path} ">下载</a>`
          }
        }
      }]
    }
  }
}
</script>
<style lang="scss" scoped>
.sync {
  text-align: right;
  a[disabled] {
    color: gray
  }
}
</style>
